package spe.ui.action;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.swt.events.HelpListener;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.IPageListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import org.eclipse.ui.internal.intro.IntroDescriptor;
import org.eclipse.ui.internal.intro.IntroMessages;
import org.eclipse.ui.internal.Workbench;


/**
 * @author: Dao Anh Vu
 * @date: May 16, 2012
 * 
 * */

public class IntroAction extends Action implements IWorkbenchAction 
{
	//private static String INTRO_TEXT = "Welcome from Dao Anh Vu";
	private IWorkbenchWindow workbenchWindow;
	private IPageListener pageListener = new IPageListener(){
		@Override
		public void pageActivated(IWorkbenchPage page) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void pageClosed(IWorkbenchPage page) 
		{
			setEnabled( workbenchWindow.getPages().length > 0 );
		}

		@Override
		public void pageOpened(IWorkbenchPage page) 
		{
			setEnabled(true);
		}
	};
	
	public IntroAction()
	{
		
	}
	
	@SuppressWarnings("restriction")
	public IntroAction(IWorkbenchWindow ww)
	{
		//super(IntroAction.INTRO_TEXT);
		super(IntroMessages.Intro_action_text);
		String label = "Dao Anh Vu";
		if(ww == null)
			throw new IllegalArgumentException();
		
		workbenchWindow = ww;
		IntroDescriptor introDescriptor = ((Workbench)workbenchWindow.getWorkbench()).getIntroDescriptor();
		
		if(introDescriptor != null)
		{
			label = introDescriptor.getLabelOverride();
			//if(label != null)
				//setText(label);
			
		}
		setText(label);
		
		setActionDefinitionId("org.eclipse.ui.help.quickStartAction");
		workbenchWindow.addPageListener(pageListener);
	}
	
	@Override
	public ImageDescriptor getImageDescriptor() 
	{
		//ImageDescriptor imgDes = RCPUIPlugin.getDefault().getImageDescriptor("full/obj16/product.gif");
		//return imgDes;
		return null;
	}

	@SuppressWarnings("restriction")
	@Override
	public void runWithEvent(Event event) 
	{
		IntroDescriptor introDescriptor = ((Workbench)workbenchWindow.getWorkbench()).getIntroDescriptor();
		
		if(introDescriptor == null)
		{
			MessageDialog.openWarning(workbenchWindow.getShell(),
					IntroMessages.Intro_missing_product_title,
					IntroMessages.Intro_missing_product_message);
			return;
		}
		
		workbenchWindow.getWorkbench().getIntroManager().showIntro(workbenchWindow, false);
	}

	@Override
	public void dispose() 
	{
		workbenchWindow.removePageListener(pageListener);
		workbenchWindow = null; //force GC
	}
}